/**
 * Stk
 * <p/>
 * User: joakimd
 * Date: 2013-03-06
 * Time: 09:40
 */
package org.dru.stk {
public final class StkMath {

    private static const MAGIC_INT: int = 16;
    private static const MAGIC_FLOOR: Number = MAGIC_INT;
    private static const MAGIC_ROUND: Number = MAGIC_INT + 0.5;

    private static var seed: int = 1;

    public function StkMath(guard: Guard) {
    }

    public static function roundUpNearestPow2(v: int): int {
        v--;
        v |= (v >> 1);
        v |= (v >> 2);
        v |= (v >> 4);
        v |= (v >> 8);
        v |= (v >> 16);
        return v + 1;
    }

    public static function floor(v: Number): Number {
        return (int)(v + MAGIC_FLOOR) - MAGIC_INT;
    }

    public static function round(v: Number): Number {
        return (int)(v + MAGIC_ROUND) - MAGIC_INT;
    }

    public static function ceil(v: Number): Number {
        return MAGIC_INT - (int)(MAGIC_FLOOR - v);
    }

    public static function clamp(v: Number): Number {
        return v - floor(v);
    }

    public static function linear(buf: Vector.<Number>, pos: Number, mask: int = -1): Number {
        const idx: int = int(pos);
        const tmp: Number = buf[idx];
        return tmp + (buf[(idx + 1) & mask] - tmp) * (pos - Number(idx));
    }

    public static function random(): Number {
        seed *= 16807;
        return Number(seed * 4.6566129e-010);
    }
}
}

class Guard {
}